package com.foodorder.controller;

import com.foodorder.service.PaymentService;
import com.foodorder.utils.ResponseResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 支付控制器
 */
@Slf4j
@RestController
@RequestMapping("/payments")
@RequiredArgsConstructor
public class PaymentController {

    private final PaymentService paymentService;

    /**
     * 生成支付参数
     */
    @PostMapping("/prepare")
    public ResponseEntity<?> preparePayment(@RequestBody Map<String, Object> paymentInfo) {
        try {
            Long orderId = Long.valueOf(paymentInfo.get("orderId").toString());
            String method = (String) paymentInfo.get("method");
            
            log.info("准备支付参数: orderId={}, method={}", orderId, method);
            
            Map<String, Object> paymentParams = paymentService.preparePayment(orderId, method);
            
            return ResponseEntity.ok(ResponseResult.success(paymentParams));
        } catch (Exception e) {
            log.error("准备支付参数失败: {}", e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ResponseResult.error("准备支付参数失败: " + e.getMessage()));
        }
    }

    /**
     * 支付结果通知
     */
    @PostMapping("/notify/{paymentMethod}")
    public ResponseEntity<?> paymentNotify(@PathVariable String paymentMethod, 
                                         @RequestBody String notifyData) {
        try {
            log.info("收到支付回调通知: method={}", paymentMethod);
            
            boolean success = paymentService.handlePaymentNotify(paymentMethod, notifyData);
            
            if (success) {
                return ResponseEntity.ok("success");  // 特殊格式，支付平台要求的响应
            } else {
                return ResponseEntity.badRequest().body("fail");
            }
        } catch (Exception e) {
            log.error("处理支付通知失败: {}", e.getMessage(), e);
            return ResponseEntity.badRequest().body("fail");
        }
    }

    /**
     * 查询支付状态
     */
    @GetMapping("/status/{orderId}")
    public ResponseEntity<?> queryPaymentStatus(@PathVariable Long orderId) {
        try {
            Map<String, Object> status = paymentService.queryPaymentStatus(orderId);
            
            return ResponseEntity.ok(ResponseResult.success(status));
        } catch (Exception e) {
            log.error("查询支付状态失败: {}", e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ResponseResult.error("查询支付状态失败: " + e.getMessage()));
        }
    }

    /**
     * 关闭支付
     */
    @PostMapping("/close/{orderId}")
    public ResponseEntity<?> closePayment(@PathVariable Long orderId) {
        try {
            boolean success = paymentService.closePayment(orderId);
            
            if (success) {
                return ResponseEntity.ok(ResponseResult.success("支付关闭成功"));
            } else {
                return ResponseEntity.badRequest()
                        .body(ResponseResult.error("支付关闭失败"));
            }
        } catch (Exception e) {
            log.error("关闭支付失败: {}", e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ResponseResult.error("关闭支付失败: " + e.getMessage()));
        }
    }

    /**
     * 申请退款
     */
    @PostMapping("/refund")
    public ResponseEntity<?> refundPayment(@RequestBody Map<String, Object> refundInfo) {
        try {
            Long orderId = Long.valueOf(refundInfo.get("orderId").toString());
            Double amount = Double.valueOf(refundInfo.get("amount").toString());
            String reason = (String) refundInfo.get("reason");
            
            log.info("申请退款: orderId={}, amount={}, reason={}", orderId, amount, reason);
            
            Map<String, Object> result = paymentService.refundPayment(orderId, amount, reason);
            
            return ResponseEntity.ok(ResponseResult.success(result));
        } catch (Exception e) {
            log.error("申请退款失败: {}", e.getMessage(), e);
            return ResponseEntity.badRequest()
                    .body(ResponseResult.error("申请退款失败: " + e.getMessage()));
        }
    }
} 